package com.eqdd.yiqidian.ui

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.inputmethod.InputMethodManager
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.rebuild.OptionBean
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.http.DialogCallBack
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.kotlin.BaseRecyclerViewAdapter
import com.eqdd.yiqidian.kotlin.toArrayList
import com.jhj.decodelibrary.CharacterUtil
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_recyclerview.*
import kotlinx.android.synthetic.main.list_item_150.view.*
import org.jetbrains.anko.toast

/**
 * Created by jhj on 18-7-21.
 */
class SingleChooseActivity : BaseActivity() {

    val adapter = ListAdapter()
    var dataList: List<OptionBean>? = null
    lateinit var character: CharacterUtil

    override fun initBinding() {
        setContentView(R.layout.activity_recyclerview)
        val title = intent.getStringExtra(Config.TITLE)
        initTopTitleBar(View.VISIBLE, title)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.addItemDecoration(DividerLineItemDecoration(this))
        recyclerView.adapter = adapter
        layout_search_bar.visibility = View.VISIBLE
        searchBarMask.setOnTouchListener(keyboardState)
        character = CharacterUtil.getInstance(this)

    }

    override fun initData() {
        val type = intent.getIntExtra(Config.TYPE, -1)
        if (type == -1) {
            toast("获取列表失败")
        }
        OkGo.post<List<OptionBean>>(HttpConfig.BASE_URL + HttpConfig.AREA_OPTIONS)
                .params("type", type)
                .execute(object : DialogCallBack<List<OptionBean>>(this) {
                    override fun onSuccess(response: Response<List<OptionBean>>?) {
                        response?.body()?.let { list ->
                            dataList = list.toArrayList()
                            adapter.dataList = list.toArrayList()
                            adapter.notifyDataSetChanged()
                            /*Observable
                                    .create<List<OptionBean>> {
                                        list.forEach { t ->
                                            t.spelling = character.getFirstSpelling(t.name)
                                            t.sortArray = character.getStringArray(t.name)
                                        }
                                        it.onNext(list)
                                    }
                                    .doOnNext {
                                        Collections.sort(it, ArraySortUtil())
                                    }
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe {
                                        dataList = it
                                        adapter.dataList = it.toArrayList()
                                        adapter.notifyDataSetChanged()
                                    }*/
                        }
                    }
                })
    }

    override fun setView() {

    }

    private val keyboardState = View.OnTouchListener { _, _ ->
        searchBarMask.visibility = View.GONE
        et_search.addTextChangedListener(textChangedListener)
        var inputManager: InputMethodManager? = null
        if (inputManager == null) {
            inputManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
            inputManager.showSoftInput(et_search, 0)
        }
        recyclerView.setOnTouchListener { _, _ ->
            (inputManager as InputMethodManager).hideSoftInputFromWindow(et_search.windowToken, 0)
            if (et_search.text.isNullOrBlank()) {
                searchBarMask.visibility = View.VISIBLE
            }
            false
        }
        false
    }

    private val textChangedListener = object : TextWatcher {
        override fun afterTextChanged(p0: Editable?) {
        }

        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        }

        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            dataList?.let {
                if (et_search.text.isNullOrBlank()) {
                    adapter.dataList = it.toArrayList()
                } else {
                    adapter.dataList = it.filter { data -> data.name.contains(et_search.text.toString()) }.toArrayList()
                }
                adapter.notifyDataSetChanged()
            }
        }

    }


    inner class ListAdapter : BaseRecyclerViewAdapter<OptionBean>() {
        override fun onCreateView(viewType: Int): Int {
            return R.layout.list_item_150
        }

        override fun onBindViewItemViewHolder(holder: ItemViewHolder, bean: OptionBean, position: Int) {
            holder.itemView.tv_content.text = bean.name
            holder.itemView.setOnClickListener {
                val intent = Intent()
                intent.putExtra("data", bean.name)
                setResult(Activity.RESULT_OK, intent)
                finish()
            }
        }

    }


}